package com.panoramagl.example;

import com.panoramagl.PLISceneElement;
import com.panoramagl.PLIView;
import com.panoramagl.PLImage;



import com.panoramagl.PLCubicPanorama;
import com.panoramagl.PLIPanorama;
import com.panoramagl.PLSpherical2Panorama;
import com.panoramagl.PLTexture;
import com.panoramagl.PLView;
import com.panoramagl.PLViewListener;
import com.panoramagl.enumerations.PLCubeFaceOrientation;
import com.panoramagl.example.R;
import com.panoramagl.hotspots.PLHotspot;
import com.panoramagl.hotspots.PLIHotspot;
import com.panoramagl.ios.UITouch;
import com.panoramagl.ios.structs.CGPoint;
import com.panoramagl.structs.PLPosition;
import com.panoramagl.transitions.PLTransitionBlend;
import com.panoramagl.utils.PLUtils;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

public class PanoramaGLActivity extends PLView
{
	/**init methods*/
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);



		Context context = this.getApplicationContext();
		PLIPanorama panorama = null;
		//Load panorama
		PLCubicPanorama cubicPanorama = new PLCubicPanorama();
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1f), false), PLCubeFaceOrientation.PLCubeFaceOrientationFront);
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1b), false), PLCubeFaceOrientation.PLCubeFaceOrientationBack);
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1l), false), PLCubeFaceOrientation.PLCubeFaceOrientationLeft);
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1r), false), PLCubeFaceOrientation.PLCubeFaceOrientationRight);
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1u), false), PLCubeFaceOrientation.PLCubeFaceOrientationUp);
    	cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.p_1d), false), PLCubeFaceOrientation.PLCubeFaceOrientationDown);
//		cubicPanorama.setImage(new PLImage(PLUtils.getBitmap(context, R.raw.fresh), false), PLCubeFaceOrientation.PLCubeFaceOrientationFront);

//		cubicPanorama.addHotspot()
//		cubicPanorama.addHotspot(new PLHotspot(1, new PLImage(PLUtils
//				.getBitmap(context, R.raw.fresh), false), 0.0f,
//				170.0f, 0.05f, 0.05f));
//
//		cubicPanorama.addHotspot(new PLHotspot(1, new PLImage(PLUtils
//				.getBitmap(context, R.raw.fresh), false), 0.0f,
//				0.0f, 0.05f, 0.05f));
//
//



		panorama = cubicPanorama;
//        if(panorama != null)
//    	{
//    		//Set camera rotation
//    		panorama.getCamera().lookAt(0.0f, 0.0f);
//	        //Reset view
//	        this.reset();
//	        //Load panorama
//	        this.startTransition(new PLTransitionBlend(2.0f), panorama); //or use this.setPanorama(panorama);
//    	}
//		PLSpherical2Panorama panorama = new PLSpherical2Panorama();
//		panorama.getCamera().lookAt(30.0f, 90.0f);
//        panorama.setImage(new PLImage(PLUtils.getBitmap(this, R.raw.spherical_pano), false));



		setLocked(true);
		panorama.getCamera().lookAt(0.0f, 10.0f);


		PLHotspot currentHotspot = new PLHotspot(1, new PLImage(PLUtils
				.getBitmap(context, R.raw.fresh), false), 0.0f,
				10.0f, 0.1f, 0.05f);


//		currentHotspot.setOnClick("123");

//		cubicPanorama.addHotspot(currentHotspot);
		panorama.addHotspot(currentHotspot);

		reset();

		Log.e("onCreate=", "===");

		setListener(new PLViewListener() {
			@Override
			public void onDidClickHotspot(PLIView view, PLIHotspot hotspot, CGPoint screenPoint, PLPosition scene3DPoint) {
				super.onDidClickHotspot(view, hotspot, screenPoint, scene3DPoint);
				Log.e("onDidClickHotspot=", "===");
			}

			@Override
			public void onDidClickElement(PLIView view, PLISceneElement element, CGPoint screenPoint, PLPosition scene3DPoint) {
				super.onDidClickElement(view, element, screenPoint, scene3DPoint);

				Log.e("onDidClickElement=", "===");
			}

			@Override
			public void onDidOverHotspot(PLIView view, PLIHotspot hotspot, CGPoint screenPoint, PLPosition scene3DPoint) {
				super.onDidOverHotspot(view, hotspot, screenPoint, scene3DPoint);
				Log.e("onDidOverHotspot=", "===");

			}

			@Override
			public void onTouchesBegan(PLIView view, List<UITouch> touches, MotionEvent event) {
				super.onTouchesBegan(view, touches, event);
				Log.e("onTouchesBegan=", view+"==="+view.getActivity()+"==="+event.getX());

//				 Toast.makeText(
//						 view.getActivity().getApplicationContext(),
//						 String.format("You select the hotspot with ID %d",
//								 hotspot.getIdentifier()), Toast.LENGTH_SHORT)
//						 .show();


			}

			//			@Override
//			public void onDidClickHotspot(PLIView view, PLIHotspot hotspot,
//										  CGPoint screenPoint, PLPosition scene3DPoint) {
//
//				Log.e("onDidClickHotspot=", "===");
//
////								 Toast.makeText(
////										 view.getActivity().getApplicationContext(),
////										 String.format("You select the hotspot with ID %d",
////												 hotspot.getIdentifier()), Toast.LENGTH_SHORT)
////										 .show();
//			}
		});

		setPanorama(panorama);
		setLocked(false);
	}
	
	/**
     * This event is fired when root content view is created
     * @param contentView current root content view
     * @return root content view that Activity will use
     */
	@Override
	protected View onContentViewCreated(View contentView)
	{
		//Load layout
		ViewGroup mainView = (ViewGroup)this.getLayoutInflater().inflate(R.layout.activity_main, null);
		//Add 360 view
    	mainView.addView(contentView, 0);
    	//Return root content view
		return super.onContentViewCreated(mainView);
	}
}